<?php
/* 
 * ******************************
	Incoming Tasks aJax actions
   ******************************
 *
 */
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$start		=	$_REQUEST['start'];
    $end		=	$_REQUEST['end'];
	
	switch ($action) {
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("
				    	    		SELECT 		sale_transaction.id,
    	    									DATE(sale_transaction.end_date),
												production.`name`,
												production_category.`name`,
												CONCAT(ROUND(SUM(recipie_producing.quantity), 3), ' ' , production_unit.`name`)
FROM 		`sale_transaction`
LEFT JOIN 	sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
INNER JOIN 	recipie_producing ON sale_transaction_detail.id = recipie_producing.transaction_detail_id
LEFT JOIN 	production ON recipie_producing.production_id = production.id
LEFT JOIN production_unit ON production.unit = production_unit.id
LEFT JOIN production_category ON production.category = production_category.id
WHERE   	sale_transaction.`status` = 0 AND (DATE(sale_transaction.end_date) >= '$start' AND DATE(sale_transaction.end_date) <= '$end')
GROUP BY  	DATE(sale_transaction.end_date), recipie_producing.production_id

UNION ALL 

SELECT 		sale_transaction.id,
    	    									DATE(sale_transaction.end_date),
												production.`name`,
													production_category.`name`,
												CONCAT(ROUND(SUM(sale_transaction_detail.product_quantity), 3), ' ' , production_unit.`name`)
FROM 		`sale_transaction`
LEFT JOIN 	sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
LEFT JOIN 	production ON sale_transaction_detail.production_id = production.id
LEFT JOIN production_unit ON production.unit = production_unit.id
LEFT JOIN production_category ON production.category = production_category.id
WHERE   	sale_transaction.`status` = 0 AND (DATE(sale_transaction.end_date) >= '$start' AND DATE(sale_transaction.end_date) <= '$end') AND production.id NOT IN (SELECT recipie.recipie_id FROM recipie) AND production.category != 1
GROUP BY  	DATE(sale_transaction.end_date), sale_transaction_detail.production_id
    	    		");
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}
			
			echo json_encode( $output );

	        break;	    
	    default:
	       	echo "null";
	}
?>